package com.one.react;

import java.util.ArrayList;
import java.util.List;

public class Observable<T> {
    private T data;
    private List<Observer<T>> observers = new ArrayList<>();

    public Observable(T data) {
        this.data = data;
    }

    public T get() {
        return data;
    }

    public void addObserver(Observer<T> observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer<T> observer) {
        observers.remove(observer);
    }

    public void removeAllObservers() {
        observers.clear();
    }

    public void update(T data) {
        this.data = data;
        for (Observer<T> observer : observers) {
            observer.onUpdate(data);
        }
    }
}
